package ink.mint.parameter;


import ink.mint.convolution.ConvolutionLayer;
import ink.mint.convolution.ConvolutionNeuralNetwork;
import ink.mint.neural.Layer;

import java.io.*;
import java.util.List;

public class Parameter implements Serializable {
    /**
     * 保存的参数相对路径
     */
    public static final String SaveParameterPath = "DataSet/";
    /**
     * 读取的参数相对路径
     */
    public static final String ReadParameterPath = "DataSet/";
    /**
     * 参数文件名
     */
    private static final String parameterFileName = "MLPNN.mode";

    /**
     * 参数全路径名（相对路径）
     */
    private String ParameterFullFileNamePath = "";

    /**
     * 参数构造函数
     *
     * @param ParameterFullFileNamePath 参数全路径名（相对路径）
     */
    public Parameter(String ParameterFullFileNamePath) {
        this.ParameterFullFileNamePath = ParameterFullFileNamePath;
    }

    /**
     * 参数序列化
     *
     * @param layerList 网络层集合（ArrayList）
     */
    public void serialize(List<Layer> layerList) {

//        String saveFullFileName = SaveParameterPath + parameterFileName;

        try {
            FileOutputStream fileOut = new FileOutputStream(ParameterFullFileNamePath);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(layerList);
            out.close();
            fileOut.close();
            System.out.println("序列化后的文件保存在：" + ParameterFullFileNamePath);
        } catch (Exception i) {
            i.printStackTrace();
        }
    }

    /**
     * 参数反序列化
     *
     * @return 网络层集合（ArrayList）
     */
    public List<Layer> deSerialize() throws IOException, ClassNotFoundException {
        List<Layer> layerList = null;
        FileInputStream fileIn = null;
//        String readFullFileName = ReadParameterPath + parameterFileName;
        try {
            fileIn = new FileInputStream(ParameterFullFileNamePath);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            layerList = (List<Layer>) in.readObject();
            return layerList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return layerList;
    }


    public void convolutionSerialize(ConvolutionNeuralNetwork convolutionNeuralNetwork) {

//        String saveFullFileName = SaveParameterPath + parameterFileName;

        try {
            FileOutputStream fileOut = new FileOutputStream(ParameterFullFileNamePath);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(convolutionNeuralNetwork);
            out.close();
            fileOut.close();
            System.out.println("序列化后的文件保存在：" + ParameterFullFileNamePath);
        } catch (Exception i) {
            i.printStackTrace();
        }
    }

    public ConvolutionNeuralNetwork deConvolutionLayerSerialize() throws IOException, ClassNotFoundException {
        List<ConvolutionLayer> convolutionLayerList = null;
        FileInputStream fileIn = null;
//        String readFullFileName = ReadParameterPath + parameterFileName;
//        try {
        fileIn = new FileInputStream(ParameterFullFileNamePath);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        ConvolutionNeuralNetwork convolutionNeuralNetwork = (ConvolutionNeuralNetwork) in.readObject();
        return convolutionNeuralNetwork;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return layerList;
    }
}
